<template>
  <div>
    <Category :scene="scene" />
    <el-card style="margin: 10px 0;">
      <div v-show="scene == 0">
        <el-button @click="addSpu" type="primary" icon="Plus"
          :disabled="categoryStore.c3Id ? false : true">添加SPU</el-button>
        <el-table style="margin: 10px 0;" border :data="records">
          <el-table-column label="序号" type="index" align="center" width="80px"></el-table-column>
          <el-table-column label="SPU名称" prop="spuName"></el-table-column>
          <el-table-column label="SPU描述" prop="description" show-overflow-tooltip></el-table-column>
          <el-table-column label="SPU操作">
            <template #="{ row, $index }">
              <el-button @click="addSku(row)" type="primary" size="small" icon="Plus" title="添加SKU"></el-button>
              <el-button @click="updateSpu(row)" type="primary" size="small" icon="Edit" title="修改SPU"></el-button>
              <el-button @click="findSku(row)" type="primary" size="small" icon="View" title="查看SKU列表"></el-button>
              <el-popconfirm @confirm="deleteSpu(row)" :title="`你确定要删除${row.spuName}吗?`" width="200px" icon="Delete">
                <template #reference>
                  <el-button type="primary" size="small" icon="Delete" title="删除SPU"></el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination v-model:currentPage="pageNo" v-model:page-size="pageSize" :page-sizes="[3, 5, 7, 9]"
          :background="true" layout="prev, pager, next, jumper,->,sizes,total" :total="total"
          @current-change="getHasSpu" @size-change="changeSize" />
      </div>
      <!-- 添加SPU|修改SPU子组件 -->
      <SpuForm ref="spu" v-show="scene == 1" @changeScene="changeScene"></SpuForm>
      <!-- 添加SKU子组件 -->
      <SkuForm ref="sku" @changeScene="changeScene" v-show="scene == 2"></SkuForm>
      <!-- 对话框 展示SKU数据 -->
      <el-dialog title="SKU列表" v-model="show">
        <el-table border :data="skuArr">
          <el-table-column label="SKU名字" prop="skuName"></el-table-column>
          <el-table-column label="SKU价格" prop="price"></el-table-column>
          <el-table-column label="SKU重量" prop="weight"></el-table-column>
          <el-table-column label="SKU图片">
            <template #="{ row, $index }">
              <img :src="row.skuDefaultImg" style="width: 100px;height: 100px;" alt="">
            </template>
          </el-table-column>
        </el-table>
      </el-dialog>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, watch, onBeforeUnmount } from 'vue';
import useCategoryStore from '@/store/modules/category';
import { reqHasSpu, reqSkuList, reqRemoveSpu } from '@/api/product/spu';
import type { SkuData, HasSpuResponseData, Records, SpuData, SkuInfoData } from '@/api/product/spu/type';
// 引入相应的子组件
import SpuForm from './spuForm.vue';
import SkuForm from './skuForm.vue';
import { ElMessage } from 'element-plus';

// 获取分类仓库数据对象
let categoryStore = useCategoryStore()

// 场景数据
let scene = ref<number>(0) //0: 显示已有的SPU, 1: 添加SPU|修改SPU, 2: 添加SKU
// 分页器默认页码
let pageNo = ref<number>(1)
// 每一页展示几条数据
let pageSize = ref<number>(3)
//存储已有的SPU总条数
let total = ref<number>(0)
// 存储已有的SPU数据
let records = ref<Records>([])
// 获取子组件实例SpuForm
let spu = ref<any>()
// 获取子组件实例sku
let sku = ref<any>()
// 存储全部的SKU数据
let skuArr = ref<SkuData[]>([])
let show = ref<boolean>(false)

// 监听三级分类id的变化
watch(() => categoryStore.c3Id, () => {
  // 务必保证有三级分类id
  if (!categoryStore.c3Id) return
  getHasSpu()
})

// 获取某一个三级分类下全部已有的SPU
const getHasSpu = async (pager = 1) => {
  pageNo.value = pager
  let result: HasSpuResponseData = await reqHasSpu(pageNo.value, pageSize.value, categoryStore.c3Id)
  if (result.code == 200) {
    records.value = result.data.records
    total.value = result.data.total
  }
}

// 分页器下来菜单发生变化
const changeSize = () => {
  getHasSpu()
}

// 添加新的SPU,切换场景1->SpuForm组件
const addSpu = () => {
  scene.value = 1
  // 点击添加SPU,调用子组件的方法初始化数据
  spu.value.initAddSpu(categoryStore.c3Id)
}

// 修改按钮
const updateSpu = (row: SpuData) => {
  scene.value = 1
  // 调用子组件实例方法获取完整已有的SPU数据
  spu.value.initHasSpuData(row)
}

// 子组件SpuForm绑定自定义事件:目前是让子组件通知父组件切换场景0
const changeScene = (obj: any) => {
  scene.value = obj.flag
  if (obj.params == 'update') {
    // 更新留在当前页
    getHasSpu(pageNo.value)
  } else {
    // 添加留在第一页
    getHasSpu()
  }

}

// 添加SKU按钮
const addSku = (row: SpuData) => {
  // 点击SKU按钮切换场景2
  scene.value = 2
  // 调用子组件的方法初始化添加SKU数据
  sku.value.initSkuData(categoryStore.c1Id, categoryStore.c2Id, row)
}

// 查看Sku列表数据
const findSku = async (row: SpuData) => {
  let result: SkuInfoData = await reqSkuList(row.id)
  if (result.code == 200) {
    skuArr.value = result.data
    // 对话框显示出来
    show.value = true
  }
}

// 删除SPU按钮
const deleteSpu = async (row: SpuData) => {
  let result: any = await reqRemoveSpu(row.id)
  if (result.code == 200) {
    ElMessage({
      type: 'success',
      message: '删除成功'
    })
    // 获取剩余的已有的SPU数据
    getHasSpu(records.value.length > 1 ? pageNo.value : pageNo.value - 1)
  } else {
    ElMessage({
      type: 'error',
      message: '删除失败'
    })
  }
}

// 路由组件销毁时,把仓库分类相关的数据清空
onBeforeUnmount(() => {
  categoryStore.$reset()
})
</script>

<style lang="scss" scoped></style>